package com.example.project.projectspringcloud.controller.admin.evaluate;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.sys.Dictionary;
import com.example.project.projectspringcloud.entity.user.Evaluate;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.service.sys.SysDictionaryService;
import com.example.project.projectspringcloud.service.user.UserEvaluateService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台评价管理")
@Controller
@RequestMapping(value = "/admin/evaluate")
public class AdminEvaluateController {

    @Autowired
    private UserEvaluateService userEvaluateService;

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "评价管理列表", notes = "返回评价管理列表")
    @RequestMapping(value = {"", "/list"})
    public String list(Evaluate evaluate, Page page, Model model) {
        // 获取编剧、摄影和视频评价
        evaluate.setTargetTypes(new Integer[]{4, 5, 6});
        List<Evaluate> evaluates = userEvaluateService.list(new Page(0, 20), evaluate);

        model.addAttribute("page", new Page<Evaluate>(evaluates));
        model.addAttribute("evaluate", evaluate);

        model.addAttribute("dictionaries", sysDictionaryService.list(new Dictionary("evaluate-target-type"), null));

        return "admin/evaluate/evaluate-list";
    }

    @ApiOperation(value = "通过订单号获取评价", notes = "返回评价表单")
    @GetMapping(value = "/form")
    public String form(@RequestParam(value = "targetType", required = false) Integer targetType,
                       @RequestParam(value = "target", required = false) Integer target, Model model) {
        if (targetType != null && target != null) {
            Evaluate evaluate = userEvaluateService.get(new Evaluate(target, targetType));
            model.addAttribute("evaluate", evaluate);

            switch (evaluate.getTargetType()) {
                case 4:
                    List<Dictionary> scriptWriterDicts = sysDictionaryService.list(new Dictionary("evaluate-script-writer-tags"), null);
                    model.addAttribute("tagDicts", scriptWriterDicts);
                    break;
                case 5:
                    List<Dictionary> photoGrapherDicts = sysDictionaryService.list(new Dictionary("evaluate-photo-grapher-tags"), null);
                    model.addAttribute("tagDicts", photoGrapherDicts);
                    break;
                case 6:
                    List<Dictionary> productionDicts = sysDictionaryService.list(new Dictionary("evaluate-production-tags"), null);
                    model.addAttribute("tagDicts", productionDicts);
                    break;
                default:
                    break;
            }
        }
        return "admin/evaluate/evaluate-form";
    }

    @ApiOperation(value = "API添加评价", notes = "用户创建评价")
    @PostMapping(value = "/api/evaluate")
    @ResponseBody
    public RespBody add(Evaluate evaluate) throws Exception {
        // 如果非匿名评价, 查找用户是否存在
        if (!evaluate.getAnonymous().equals(1)) {
            User user = userService.get(new User(evaluate.getUserId()));
            if (user == null) {
                return RespBody.error(Constants.Message.USER_ACCOUNT_NOT_FOUND);
            }
        }
        userEvaluateService.save(evaluate);
        return RespBody.ok(true);
    }

    @ApiOperation(value = "API删除", notes = "delete by id/ids")
    @DeleteMapping(value = "/api/delete/{ids}")
    @ResponseBody
    public RespBody delete(@ApiParam(value = "字符串ID数组 1,2,3...") @PathVariable("ids") String ids) {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        Integer[] idarr = StringUtil.split(ids, ",");
        if (!userEvaluateService.delete(idarr)) {
            return RespBody.error(Constants.Message.DELETE_FAILED);
        }
        return RespBody.ok(Constants.Message.DELETE_SUCCESS);
    }
}
